# 在 app 目录下创建 management/commands/create_admin.py
from django.core.management.base import BaseCommand
from account.models import User
import os


class Command(BaseCommand):
    help = '创建带密码的超级管理员'
    print(help)

    def handle(self, *args, **options):
        username = 'admin'
        email = 'admin@example.com'
        password = '123456'
        # password = os.environ.get('ADMIN_PASSWORD')  # 从环境变量获取密码

        if not password:
            self.stdout.write(self.style.ERROR('请设置 ADMIN_PASSWORD 环境变量'))
            return

        if User.objects.filter(username=username).exists():
            self.stdout.write(self.style.SUCCESS('超级用户已存在'))
            print('用户已经存在')
            return

        User.objects.create_superuser(username, email, password)
        self.stdout.write(self.style.SUCCESS(f'超级用户 {username} 创建成功'))
        print('创建成功！')
